package com.pengweiwei.androidzygote.video;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentManager;

import com.google.android.material.tabs.TabLayout;
import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.databinding.FragmentHomeVideoBinding;
import com.pengweiwei.androidzygote.video.data.VideoApiHelper;
import com.pengweiwei.androidzygote.video.data.beans.FooterBean;
import com.putong35.common.account.AccountManager;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.fragment.BaseFragment;
import com.putong35.common.httprequest.OnFailureListener;
import com.putong35.common.httprequest.OnSuccessListener;
import com.putong35.common.utils.EasyToast;

import java.util.List;

@SuppressLint("ValidFragment")
public class HomeVideoFragment extends BaseFragment {

    public HomeVideoFragment() {

    }


    private String currChannel = "";
    private String sort = sortArr[0]; // 默认按点赞排序

    FragmentHomeVideoBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initView();



        setState(CompState.EMPTY_REFRESHING);
        VideoApiHelper.footer(3, new OnSuccessListener<List<FooterBean>>() {
            @Override
            public void onSuccess(int code, String msg, List<FooterBean> data, Object extObj) {
                if (data != null && data.size() > 0) {
                    TabLayout tabMain = binding.tabMain;
                    for (int i = 0; i < data.size(); i++) {
                        FooterBean bean = data.get(i);
                        tabMain.addTab(tabMain.newTab().setText(bean.getName()));
                    }
                    tabMain.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            int posi = tabMain.getSelectedTabPosition();
                            currChannel = data.get(posi).getId();
                            onSelectChanged();
                        }

                        @Override
                        public void onTabUnselected(TabLayout.Tab tab) {

                        }

                        @Override
                        public void onTabReselected(TabLayout.Tab tab) {

                        }
                    });
//                    tabMain.getTabAt(0).select();
                    currChannel = data.get(0).getId();
                    onSelectChanged();
                }

                setState(CompState.DATA);
            }
        }, new OnFailureListener() {
            @Override
            public void onFailure(int errCode, String msg) {
                EasyToast.showToast(getActivity(), msg);
//                setState(CompState.EMPTY_ERROR);
            }
        });



        return view;
    }

    VideoGridFragment dataFragment = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeVideoBinding.inflate(inflater);
        binding.setAccount(AccountManager.getInstance().getAccount());
        FragmentManager fragmentManager = getChildFragmentManager();
        dataFragment  = (VideoGridFragment) fragmentManager.findFragmentById(R.id.fragData);

        return binding.getRoot();
    }


    private static String[] sortArr = new String[]{"a.like_num%20desc","a.like_num%20desc","a.like_num%20desc"};
    private void initView() {
        TabLayout tabSub = binding.tabSub;
        tabSub.addTab(tabSub.newTab().setText("点赞排行"));
        tabSub.addTab(tabSub.newTab().setText("播放排行"));
        tabSub.addTab(tabSub.newTab().setText("最新排行"));
        tabSub.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int posi = tabSub.getSelectedTabPosition();
                sort = sortArr[posi];
                onSelectChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void onSelectChanged() {
        dataFragment.onSelectChanged(sort, currChannel);
    }

}
